package com.example.calculator;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEtInput;
    private LinearLayout mLlKey;
    private Button mBtC;
    private Button mBt7;
    private Button mBt4;
    private Button mBt1;
    private Button mBtPercent;
    private Button mBtDel;
    private Button mBt8;
    private Button mBt5;
    private Button mBt2;
    private Button mBt0;
    private Button mBtDivider;
    private Button mBt9;
    private Button mBt6;
    private Button mBt3;
    private Button mBtPoint;
    private Button mBtMult;
    private Button mBtMinus;
    private Button mBtAdd;
    private Button mBtEq;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        选中布局 alt+insert呼出 快捷操作
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mEtInput = (EditText) findViewById(R.id.et_input);
        mBtC = (Button) findViewById(R.id.bt_c);
        mBtC.setOnClickListener(this);
        mBt7 = (Button) findViewById(R.id.bt_7);
        mBt7.setOnClickListener(this);
        mBt4 = (Button) findViewById(R.id.bt_4);
        mBt4.setOnClickListener(this);
        mBt1 = (Button) findViewById(R.id.bt_1);
        mBt1.setOnClickListener(this);
        mBtPercent = (Button) findViewById(R.id.bt_percent);
        mBtPercent.setOnClickListener(this);
        mBtDel = (Button) findViewById(R.id.bt_del);
        mBtDel.setOnClickListener(this);
        mBt8 = (Button) findViewById(R.id.bt_8);
        mBt8.setOnClickListener(this);
        mBt5 = (Button) findViewById(R.id.bt_5);
        mBt5.setOnClickListener(this);
        mBt2 = (Button) findViewById(R.id.bt_2);
        mBt2.setOnClickListener(this);
        mBt0 = (Button) findViewById(R.id.bt_0);
        mBt0.setOnClickListener(this);
        mBtDivider = (Button) findViewById(R.id.bt_divider);
        mBtDivider.setOnClickListener(this);
        mBt9 = (Button) findViewById(R.id.bt_9);
        mBt9.setOnClickListener(this);
        mBt6 = (Button) findViewById(R.id.bt_6);
        mBt6.setOnClickListener(this);
        mBt3 = (Button) findViewById(R.id.bt3);
        mBt3.setOnClickListener(this);
        mBtPoint = (Button) findViewById(R.id.bt_point);
        mBtPoint.setOnClickListener(this);
        mBtMult = (Button) findViewById(R.id.bt_mult);
        mBtMult.setOnClickListener(this);
        mBtMinus = (Button) findViewById(R.id.bt_minus);
        mBtMinus.setOnClickListener(this);
        mBtAdd = (Button) findViewById(R.id.bt_add);
        mBtAdd.setOnClickListener(this);
        mBtEq = (Button) findViewById(R.id.bt_eq);
        mBtEq.setOnClickListener(this);
        mLlKey = (LinearLayout) findViewById(R.id.ll_key);

    }

    @Override
    public void onClick(View v) {
        String str = mEtInput.getText().toString();
        switch (v.getId()) {
            case R.id.bt_c:
                mEtInput.setText("");
                break;
            case R.id.bt_del:
                //
                if (TextUtils.isEmpty(str)) {
                    return;
                }
                //abc[)
                mEtInput.setText(str.substring(0, str.length() - 1));
                break;
            case R.id.bt_mult:
            case R.id.bt_divider:
            case R.id.bt_minus:
            case R.id.bt_add:
            case R.id.bt_percent:
                //1+1 给拼接上
                mEtInput.setText(str + " " + ((Button) v).getText().toString() + " ");
                break;
            case R.id.bt_7:
            case R.id.bt_4:
            case R.id.bt_1:
            case R.id.bt_8:
            case R.id.bt_5:
            case R.id.bt_2:
            case R.id.bt_0:
            case R.id.bt_9:
            case R.id.bt_6:
            case R.id.bt3:
            case R.id.bt_point:
                mEtInput.setText(str + ((Button) v).getText().toString());
                break;
            case R.id.bt_eq:
                getReult();
                break;
        }

    }

    private void getReult() {
        //1+1
        //通过输入框里面的内容 把结果返回
        String str = mEtInput.getText().toString();
        //1 + 1
        if (TextUtils.isEmpty(str)) {
            return;
        }
        //运算符前面的字符串
        String s1 = str.substring(0, str.indexOf(" "));
        //运算符
        String op = str.substring(str.indexOf(" ") + 1, str.indexOf(" ") + 2);
        //运算符前后面的字符串
        String s2 = str.substring(str.indexOf(" ") + 3, str.length());

        if (TextUtils.isEmpty(s1)) {
            Toast.makeText(this, "运算符前面的字符串 不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(op)) {
            Toast.makeText(this, "运算符 不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(s2)) {
            Toast.makeText(this, "运算符后面的字符串 不正确", Toast.LENGTH_SHORT).show();
            return;
        }
        double result = 0;
        double d1 = Double.parseDouble(s1);
        //ctrl+d快速复制一行
        double d2 = Double.parseDouble(s2);
        if (op.equals("+")) {
            result = d1 + d2;
        } else if (op.equals("-")) {
            result = d1 - d2;
        } else if (op.equals("*")) {
            result = d1 * d2;
        } else if (op.equals("/")) {
            if (d2 == 0) {
                Toast.makeText(this, "运算符后面的字符串 不能为0", Toast.LENGTH_SHORT).show();
            }
            result = d1 / d2;
        }


        mEtInput.setText(result + "");

    }

    private static final String TAG = "MainActivity";
}
